package domain.service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.datanucleus.store.mapped.expression.ScalarExpression.IllegalOperationException;

import domain.model.Domain;

public class Utils {

	public static String fomatDomain(String str) {
		if (str != null)
			str = str.toLowerCase().replaceAll("(http|https)+://(www.)*", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "").replaceAll("www.", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return str;
	}
	public static String md5(String str) throws NoSuchAlgorithmException {
		MessageDigest md;

		md = MessageDigest.getInstance("MD5");
		md.update(str.getBytes());

		byte byteData[] = md.digest();

		// convert the byte to hex format method 1
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteData.length; i++) {
			sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
					.substring(1));
		}

		str = sb.toString();
		return str;
	}

	public static String returnWhois(String str)
			throws NoSuchAlgorithmException {
		Pattern p = Pattern.compile("[^ :]+@[^ ]+");

		Matcher m = p.matcher(str.replaceAll("\\<.*?\\>", " "));
		String email = "";
		while (m.find()) {
			email = m.group().toLowerCase().trim().replaceAll("\\(", "")
					.replaceAll("\\)", "");
			str = str
					.replaceAll(
							email,
							"<img src='http://source.domaintools.com/email.pgif?md5="
									+ md5(email)
									+ "&face=arial&size=9&color=000000&bgcolor=FFFFFF&face=arial&size=9&color=0000FF&bgcolor=FFFFFF&format[]=underline&format[]=transparent&format[]=transparent'/>");
		}
		return str;
	}
	public static Boolean checkDomain(String domain)
	{
		ResourceBundle resource = ResourceBundle.getBundle("domain", new Locale("en"));
		String re_domain = domain;

    	String[] listDomain = resource.getString("domain").split(" ");
    	for(int i=0;i<listDomain.length;i++)
    	{
    		re_domain = re_domain.replaceAll("[.]{1}"+listDomain[i]+"$", "");
    	}

        
        if(re_domain.length() != domain.length())
        {
        	return true;
        }
		
		return false;
	}
	public static String textCrop(String content,String begin, String end)
	{
		 int start = content.indexOf(begin)+begin.length();
		    
		 int finish = content.indexOf(end,start); 
		 return content.substring(start, finish);
		    
		
	}
	public static String ValidDomain()
	{
		 return "find-domains.org";
	}
	public static String convertToString(List list)
	{
		
		String str="";
		for(Object jString : list)
		{
			str+=jString.toString()+",";
			
		}
		return str;
	}
	public static int checkLengthDomain(String domain)
	{
		try
        {
			ResourceBundle resource = ResourceBundle.getBundle("language", new Locale("en"));
			String re_domain = domain;
			re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");

        	String[] check = resource.getString("domain").split(" ");
        	for(int i=0;i<check.length;i++)
        	{
        		re_domain = re_domain.replaceAll(check[i], "");
        	}

	        
	        if(re_domain.length() != domain.length())
	        {
	        	return re_domain.replaceAll("[.]+$", "").length();
	        } else {
	        	return re_domain.replaceAll("[.]{1}[a-zA-Z]+$", "").length();
	        }
        }
        catch (IllegalOperationException ioe)
	    {
	        ioe.printStackTrace();
	    }
		return 0;
	}
	public static String returnDomain(String domain)
	{
		try
        {
			String re_domain = domain;
			re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
			ResourceBundle resource = ResourceBundle.getBundle("language", new Locale("en"));
        	String[] check = resource.getString("domain").split(" ");
        	for(int i=0;i<check.length;i++)
        	{
        		re_domain = re_domain.replaceAll(check[i], "");
        	}

	        
	        if(re_domain.length() != domain.length())
	        {
	        	return re_domain.replaceAll("[.]+$", "").replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "");
	        } else {
	        	return re_domain.replaceAll("[.]{1}[a-zA-Z]+$", "").replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "");
	        }
        }
        catch (IllegalOperationException ioe)
	    {
	        ioe.printStackTrace();
	    }
		return domain;
	}
	public static String returnSubDomain(String domain)
	{
		try
        {
			String re_domain = domain;
			re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
			ResourceBundle resource = ResourceBundle.getBundle("language", new Locale("en"));
        	String[] check = resource.getString("domain").split(" ");
        	for(int i=0;i<check.length;i++)
        	{
        		re_domain = re_domain.replaceAll(check[i], "");
        	}

	        if(re_domain.length() != domain.length())
	        {
	        	re_domain = re_domain.replaceAll("[.]+$", "");
	        	String str = re_domain.replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "");
	        	if(str.length() != re_domain.length())
	        	{
	        		return " is sub domain of <strong>"+domain.replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "")+"</strong>";
	        	} else {
	        		return " is top level domain";
	        	}	        	
	        } else {
	        	re_domain = re_domain.replaceAll("[.]{1}[a-zA-Z]+$", "");
	        	String str = re_domain.replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "");
	        	if(str.length() != re_domain.length())
	        	{
	        		return " is sub domain of <strong>"+domain.replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "")+"</strong>";
	        	} else {
	        		return " is top level domain";
	        	}	
	        }
        }
        catch (IllegalOperationException ioe)
	    {
	        ioe.printStackTrace();
	    }
		return domain;
	}
	public static String convert2DomainString(List<Domain> list)
	{		
		String str="";
		for(int i=0;i<list.size();i++)
		{
			str+="<a href=\"/domain/"+list.get(i).getName()+"\">"+list.get(i).getName()+"</a> , ";
			if(i >= 14)break;	
		}
		return str;
	}
	public static String cutString(String str,int count)
	{		
		String string = str;
		if(str.length() > count)
		{
			string = str.substring(0,count);
		}
		return string;
	}
	
}
